﻿using FreeRedis;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Common.Cache
{
    /// <summary>
    /// 分布式锁
    /// </summary>
    public interface IDistributedLock : IDisposable
    {
        /// <summary>
        /// 释放锁
        /// </summary>
        void Complete();
    }
    /// <summary>
    /// FreeRedis分布式锁
    /// </summary>
    public class FreeRedisDistributedLock : IDistributedLock
    {
        private readonly RedisClient.LockController adapter;
        /// <summary>
        /// FreeRedis分布式锁
        /// </summary>
        /// <param name="adapter"></param>
        public FreeRedisDistributedLock(RedisClient.LockController adapter)
        {
            this.adapter = adapter;
        }
        /// <inheritdoc/>
        public void Complete()
        {
            adapter.Unlock();
        }
        /// <summary>
        /// 释放锁
        /// </summary>
        public void Dispose() => this.Complete();
    }
}
